package bookez.view.bean.backing;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import bookez.model.businessobject.BusType;
import bookez.model.exception.BusinessLogicLayerException;
import bookez.view.bean.BaseBean;


@ManagedBean
@SessionScoped
public class BusTypeViewBox extends BaseBean implements Converter {
	
	public BusTypeViewBox() {
		//initList();
	}//end default constructor
	
	@SuppressWarnings("unused")
	@PostConstruct
	private void initList() { 
		try {
			List<BusType> result = getServiceLocator().getBusTypeService().getAllBusType();
			setList( result );
		}//end try
		
		catch ( BusinessLogicLayerException ex ) {
			ex.printStackTrace();
		}//end catch
	}//end method initList
	
	public void selectChanged( ValueChangeEvent event ) {
		
		BusType newValue = (BusType) event.getNewValue();
		
		getTypeNameInput().setValue( newValue.getName() );
		getSeatTypeInput().setValue( newValue.getSeatType() );
		getSeatCountInput().setValue( newValue.getSeatCount() );
		
		setSelected( newValue );
			
	}//end method processValueChange
	
	/*zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
	 *  Implementing interface Converter 	
	 *zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz*/
	
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
		for( BusType type : getList() )  {
			if( type.getName().equals( newValue ) ) {
				return type;
			}
		}
		return null;
	}//end method getAsObject
	

	@Override
	public String getAsString(FacesContext context, UIComponent ui, Object value ) {
		// TODO Auto-generated method stub
		BusType type = (BusType)value;
		return type.getName();
	}//end method getAsString
	
	
	public List<BusType> getList() { return this.list; }
	protected void setList( List<BusType> value ) { this.list = value; }
	
	public BusType getSelected() { return this.selected; }
	public void setSelected( BusType value ) { this.selected = value; }
	
	public ArrayList<SelectItem> getBusTypes() {
		
		ArrayList<SelectItem> items = new ArrayList<SelectItem>( getList().size() );
		
		for( BusType type : getList() ) {
			items.add( new SelectItem( type, type.getName() ) );
		}//end for
		
		return items;
		
	}//end method getBusTypes
	
	public UIInput getTypeNameInput() { return this.typeName; }
	public void setTypeNameInput( UIInput value ) { this.typeName = value; }
	
	public UIInput getSeatTypeInput() { return this.seatType; }
	public void setSeatTypeInput( UIInput value ) { this.seatType = value; }
	
	public UIInput getSeatCountInput() { return this.seatCount; }
	public void setSeatCountInput( UIInput value ) { this.seatCount = value; }
		
	private UIInput typeName;
	private UIInput seatType;
	private UIInput seatCount;
	
	
	public BusType selected;	
	private List<BusType> list;
	
	private static final long serialVersionUID = 5654253603295638135L;
}//end method BusTypeListing
